import { combineResolvers } from 'graphql-resolvers'
import * as $cos from '../../services/cos'

module.exports = {
  Query: {
    getMaterialById: combineResolvers(
      async (root, { id }, { models: { Material } }) => {
        let material = await Material.findById(id)
        return material
      }
    )
  },
  Mutation: {
    createMaterial: combineResolvers(
      async (root, { material }, { models: { Material }, user}) => {
        material.userId = user && user.id || 0
        if (material.type === 'video') {
          try {
            let coverPicKey = await $cos.genVideoCoverPic(material.key)
            material.meta = {
              coverPicKey
            }
          } catch (e) {
            console.info(e)
          }
        }
        let nm = await Material.create(material)
        return nm
      }
    )
  }
}
